/// 购物车 {skuid:num}
var addBag = function(addSkuObj, callBack){
  // 加入购物车, 商品要合并
  var num = 0;
  for(var i in addSkuObj){
     num += addSkuObj[i];
  }
    let tableID = getApp().globalData.TABLE_ID_SHOPPING_CART;
    // 找到该用户购物车所有数据
    let objects = {
        tableID: tableID,
        created_by: getApp().globalData.userInfo.userId
    };
    wx.BaaS.getRecordList(objects).then( (res) => {
        // success
        var oldObjects = res.data.objects;

        var editObj = {}; // recordId: num
        for(var i = 0; i < res.data.objects.length; i++){
            if(addSkuObj[res.data.objects[i].response.product_sku_id]){
                // 存在 更新数量
                editObj[res.data.objects[i].id] = res.data.objects[i].response.quantity + addSkuObj[res.data.objects[i].response.product_sku_id];
                delete addSkuObj[res.data.objects[i].response.product_sku_id];
            }
        }

        if(editObj){
            // 修改的记录
            var data = {};
            var editObjects = {tableID: tableID};
            for(var i in editObj){
                editObjects['recordID'] = i;
                data['quantity'] = parseInt(editObj[i]);
                editObjects['data'] = data;
                wx.BaaS.updateRecord(editObjects).then( (res) => {
                    // success
                }, (err) => {
                    // err
                });
            }
        }
        if(addSkuObj){
            // 增加的记录
            for(var i in addSkuObj){
                wx.BaaS.createRecord({
                  tableID: tableID,
                  data: {product_sku_id: i, quantity: parseInt(addSkuObj[i])}
                }).then( (res) => {
                    // success
                }, (err) => {
                    // err
                });
            }
        }
     
        callBack(num);
    }, (err) => {
        // err
    });
}
var delBag = function(shopCartId, callBack){
  // 删除商品
  let tableID = getApp().globalData.TABLE_ID_SHOPPING_CART;
  let recordID = shopCartId;
  let objects = {
    tableID,
    recordID
  };
  wx.BaaS.deleteRecord(objects).then( (res) => {
    // success
    callBack();
  }, (err) => {
    // err
  });
}
var updateBag = function(shopCartId, num, callBack){
  // 更新 ShoppingCart表
  if(num < 1){
    delBag(shopCartId, callBack);
  }else{
    let tableID = getApp().globalData.TABLE_ID_SHOPPING_CART;
    let recordID = shopCartId;
    let data = {quantity: num};
    let objects = {
      tableID,
      recordID,
      data
    };

    wx.BaaS.updateRecord(objects).then( (res) => {
      // success
      callBack(true);
    }, (err) => {
      // err
    });
  }
}
var listBag = function(that){
  // 获取购物袋列表 找到所有该用户增加的购物车，然后每条去找对应的产品ID和SKU title,最后根据产品ID找到产品相关信息
  let objects = {
    tableID: getApp().globalData.TABLE_ID_SHOPPING_CART,
    created_by: getApp().globalData.userInfo.userId
  };
  wx.BaaS.getRecordList(objects).then( (res) => {
    // success
    that.data.bagList =  res.data.objects;
    for(var i = 0; i < res.data.objects.length; i++){
      var product_sku_id = res.data.objects[i].response.product_sku_id;
      // 找到对应的sku title 和 产品ID
      wx.BaaS.getRecord({tableID:getApp().globalData.TABLE_ID_PRODUCT_SKU, recordID: product_sku_id}).then( (sku_res) => {
        // success
        that.data.skuObj[sku_res.data.id] = sku_res.data.response;
        
        var product_id = sku_res.data.response.product_id;
        wx.BaaS.getRecord({tableID:getApp().globalData.TABLE_ID_PRODUCT, recordID: product_id}).then( (product_res) => {
            that.data.productObj[product_res.data.id] = product_res.data.response;
            that.setData({productObj: that.data.productObj});
        },(err) => {

        });
        that.setData({skuObj: that.data.skuObj});
      }, (err) => {
        // err
      });
    }
    that.setData({bagList: that.data.bagList});
    
  }, (err) => {
    // err
  });
  
}
var infoBag = function(callBack){
  // 获取购物袋商品数量
  let objects = {
    tableID: getApp().globalData.TABLE_ID_SHOPPING_CART,
    created_by: getApp().globalData.userInfo.userId
  };
  wx.BaaS.getRecordList(objects).then( (res) => {
    // success
    var num = 0;
    for(var i = 0; i < res.data.objects.length; i++){
        num += typeof res.data.objects[i].response.quantity == 'undefined' ? 0 : res.data.objects[i].response.quantity;
    }
    callBack(num);
  }, (err) => {
    // err
  });
}
module.exports = {
  infoBag: infoBag,
  listBag: listBag,
  addBag: addBag,
  delBag: delBag,
  updateBag: updateBag,
}